package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> pageResponse = nursingProjectMapper.getByPage(name, status);
        PageResponse<NursingProjectVo> nursingProjectVoPageResponse = PageResponse.of(pageResponse, NursingProjectVo.class);
        List<NursingProjectVo> nursingProjectVosConst = nursingProjectVoPageResponse.getRecords();
        for (NursingProjectVo nursingProjectVo : nursingProjectVosConst) {
            Long id = nursingProjectVo.getId();
            Integer count = nursingProjectMapper.count(id);
            nursingProjectVo.setCount(count);
        }
        return nursingProjectVoPageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insert(bean);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.update(bean);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> nursingProjectList = nursingProjectMapper.getAll();
        for (NursingProjectVo nursingProjectVo : nursingProjectList) {
            Long id = nursingProjectVo.getId();
            Integer count = nursingProjectMapper.count(id);
            nursingProjectVo.setCount(count);
        }
        return nursingProjectList;
    }
}
